var eventList = {flag:"show",geteventaction:false,inter:0};
var eventData = {ids:null,len:0,index:0,status:"init",detail:null,ajaxstatus:null};
$(function(){
	init();
}); 

function init(){
	getevent();
	eventList.inter = setInterval("circle()",5000);
	setdivlink();
}

setdivlink = function(){
	$(".eventtag").click(function(){
		var key = $(".eventtag").attr("data-id");
		if(key){
			window.location = site.web+"my/cparty?"+"eid="+key;
		}
	});
}

eventaction = function(){//切换效果
	if(eventList.flag =="show"){
		display("eventtable",eventList.flag);
		display("followertable",eventList.flag);
		eventList.flag = "hide";
	}else{
		display("eventtable",eventList.flag);
		display("followertable",eventList.flag);
		eventList.flag = "show";
	}
}

getevent = function(){
	//获取活动信息
	if(eventData.status=="init"){
		eventData.status = "get";
		cb = function (data){
			if(data.code==1){
				eventData.ids = data.data;
				eventData.len = data.data.length;
				//构造container
				var arg = [],status = [];
				for(var n in eventData.ids){
					arg.push(null);
					status.push(false);
				}
				eventData.detail = arg;
				eventData.ajaxstatus = status;
				eventList.geteventaction = true;//启动活动循环展示功能
			}
		}
		var url = site.web+"my/event/geteventbyuser";
		var json = {url:url,uid:site.uid};
		G(json,cb);
	}
	
}

circle = function(){
	//循环显示活动信息
	if(eventData.ids && eventList.geteventaction){
		
		if(eventData.index==eventData.len-1){
			eventData.index = 0;
		}else{
			eventData.index ++;
		}
		label = function(arg){
			return arg;
			//return "<span class='label label-info'>"+arg+"</span>";
		}
		if(eventData.ajaxstatus[eventData.index]){
			showdatabydata(eventData.index);
			return;
		}
		//隐藏活动信息
		eventaction();
		
		cb = function (data){
			if(data.code==1){
				//set event
				var event = data.data.lastest_event;
				$(".event_title").html(event.name);
				$(".event_date").html(label("活动时间:"+timeformat(event.startdate)));
				$(".event_focus").html(label(data.data.lastest_eventfollow+"个关注"));
				$(".eventtag").attr("data-id",event.eid);
				//set follower
				$(".follower_title").html(event.name+"来宾信息");
				if(data.data.lastest_eventuser!=0){
					$(".follower_focus").html(data.data.lastest_eventuser+"位来宾");
				}else{
					$(".follower_focus").html(" 暂时没有来宾");
				}
				//insert data into eventData.detail
				eventData.detail[eventData.index] = data.data;
				eventData.ajaxstatus[eventData.index] = true;
			}else{
				//alertx("ERROR dashboard 404",data.message);
			}
			eventaction();
		}
		var url = site.web+"my/dashboard/geteventdetail";
		var json = {url:url,uid:site.uid,eid:eventData.ids[eventData.index]};
		G(json,cb);
	}
}

showdatabydata = function(key){
	//set event
	var event = eventData.detail[key].lastest_event;
	var info = eventData.detail[key];
	$(".event_title").html(event.name);
	$(".event_date").html(label("活动时间:"+timeformat(event.startdate)));
	$(".event_focus").html(label(info.lastest_eventfollow+"个关注"));
	$(".eventtag").attr("data-id",event.eid);
	//set follower
	$(".follower_title").html(event.name+"来宾信息");
	if(info.lastest_eventuser!=0){
		$(".follower_focus").html(info.lastest_eventuser+"位来宾");
	}else{
		$(".follower_focus").html(" 暂时没有来宾");
	}		
}

display = function (obj,key,timezone){
	var fn 		= $("."+obj),
		opacity = 1,
		top 	= "0px";
	if(!timezone)timezone = 700;
	if(!key)key = "hide";
	if(key=="show"){
		opacity = 0.1;
		top = "+=400px";
	}
	$(fn).animate({
		opacity: opacity,
		marginTop: top
	},timezone);
}